/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.Config;
import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.Opcodes;
import l1j.jrwz.server.datatables.ChatLogTable;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.serverpackets.S_ChatPacket;
import l1j.jrwz.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來的密語封包
 */
public class C_ChatWhisper extends ClientBasePacket {

    private static final String C_CHAT_WHISPER = "[C] C_ChatWhisper";

    private static Logger _log = Logger
            .getLogger(C_ChatWhisper.class.getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);
            String targetName = readS();
            String text = readS();
            L1PcInstance pc = client.getActiveChar();
            if (pc == null) {
                return;
            }
            // 被魔封
            if (pc.hasSkillEffect(1005)) {
                pc.sendPackets(new S_ServerMessage(242)); // 你從現在被禁止閒談。
                return;
            }
            // 等級不夠
            if (pc.getLevel() < Config.WHISPER_CHAT_LEVEL) {
                pc.sendPackets(new S_ServerMessage(404, String
                        .valueOf(Config.WHISPER_CHAT_LEVEL))); // 等級 %0 以下無法使用密談。
                return;
            }
            L1PcInstance whisperTo = L1World.getInstance()
                    .getPlayer(targetName);
            // 密語對象不存在
            if (whisperTo == null) {
                pc.sendPackets(new S_ServerMessage(73, targetName)); // \f1%0%d 不在線上。
                return;
            }
            // 自己跟自己說話
            if (whisperTo.equals(pc)) {
                return;
            }
            // 斷絕密語
            if (whisperTo.getExcludingList().contains(pc.getName())) {
                pc.sendPackets(new S_ServerMessage(117, whisperTo.getName())); // %0%s 斷絕你的密語。
                return;
            }
            // 關閉密語
            if (!whisperTo.isCanWhisper()) {
                pc.sendPackets(new S_ServerMessage(205, whisperTo.getName())); // \f1%0%d 目前關閉悄悄話。
                return;
            }
            if (text.length() > 52) { // 限制聊天字数(密语下、会根据对方名字长短 可打出的字数相应减少、所有符号等相加总共52个)
                text = text.substring(0, 52); // begin - end
                // whisperFrom
                // .sendPackets(new S_SystemMessage("限制字数：" + text.length()));
            }
            // whisperFrom.sendPackets(new S_SystemMessage("测试字数：" + text.length()));
            ChatLogTable.getInstance().storeChat(pc, whisperTo, text, 1);
            pc.sendPackets(new S_ChatPacket(whisperTo, text,
                    Opcodes.S_OPCODE_GLOBALCHAT, 9));
            whisperTo.sendPackets(new S_ChatPacket(pc, text,
                    Opcodes.S_OPCODE_WHISPERCHAT, 16));
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_CHAT_WHISPER;
    }
}
